import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Test {
    public int minSubArrayLen(int target, int[] nums) {
        int len = Integer.MAX_VALUE;
        int sum = 0;
        for(int left = 0,right = 0; right < nums.length; right++) {
            //进窗口
            sum += nums[right];

            //出窗口
            while(sum >= target) {
                len = Math.min(len, right - left + 1);
                sum -= nums[left++];
            }
        }

        return len == Integer.MAX_VALUE ? 0 : len;
    }

    public int lengthOfLongestSubstring(String s) {
        if(s == null) {
            return 0;
        }
        Set<Character> set = new HashSet<>();
        int len = Integer.MIN_VALUE;

        for(int left = 0, right = 0; right < s.length(); right++) {
            char ch = s.charAt(right);
            while(set.contains(ch)) {
                set.remove(s.charAt(left++));
            }
            set.add(ch);
            len = Math.max(len, right - left + 1);
        }
        return len == Integer.MIN_VALUE ? 0 : len;
    }

    public static int longestOnes(int[] nums, int k) {
        int count = k;
        int len = 0;
        for(int left = 0, right = 0; right < nums.length; right++) {
            if(nums[right] == 0 && count != 0) {
                count--;
            } else if(nums[right] == 0 && count == 0) {
                while(nums[left++] != 0) {;}
                count = k - 1;
            }
            len = Math.max(len,right - left + 1);
        }

        return len;
    }

    public static void main(String[] args) {
        int[] nums = {0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1};
        longestOnes(nums,2);

    }
}
